'use strict'

let Koa = require('koa')
let parser = require('koa-parser')
let router = require('koa-router')()
let fs = require('fs')



let app = new Koa()
let path = require('path')


app.use(parser())
app.use(router.routes())
// let user = {
//     admin: 123456
// }

function index() {
    router.get('/', async (ctx, next) => {
        ctx.body = "这是默认页✌"
    })
}
index()

router.get('/No', async (ctx, next) => {
    ctx.body = `
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>哭了</title>
    </head>
    <body>
        <h1>你就这么忍心拒绝我？</h1>
    </body>
    </html>
    `
})
router.get('/Login', async (ctx, next) => {
    ctx.body = `
    <html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form action="http://localhost:7777/Login" method="POST">
        <label>username</label><input type="text" name="username"><br>
        <label>password</label><input type="password" name="password"><br>
        <input type="submit" value="提交">
    </form>
</body>
</html>
    `
})
router.post('/Login', async (ctx, next) => {
    let username = ctx.request.body.username || '';
    let password = ctx.request.body.password || '';

    console.log(`signin with name: ${username}, password: ${password}`);
    if (username === 'Fox' && password === '123456') {
        ctx.body = `
        <h1>Welcome, ${username}!</h1>
        `
    } else {
        ctx.response.body = `<h1>Login failed!</h1>
        <p><a href="/Login">Try again</a></p>`;
    }
})


let port = 7777

app.listen(port)

console.log(`http://localhost:${port}`);